package com.uobgroup.ews.uam.core.domain;

import com.uobgroup.ews.uam.core.dao.SecurityGroupDao;
import com.uobgroup.ews.uam.core.service.SecurityGroupService;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Component
@Configurable
public class SecurityGroupDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<SecurityGroup> data;

	@Autowired
    SecurityGroupService securityGroupService;

	@Autowired
    SecurityGroupDao securityGroupDao;

	public SecurityGroup getNewTransientSecurityGroup(int index) {
        SecurityGroup obj = new SecurityGroup();
        setDN(obj, index);
        setDescription(obj, index);
        setDisplayName(obj, index);
        return obj;
    }

	public void setDN(SecurityGroup obj, int index) {
        String DN = "DN_" + index;
        if (DN.length() > 255) {
            DN = DN.substring(0, 255);
        }
        obj.setDN(DN);
    }

	public void setDescription(SecurityGroup obj, int index) {
        String description = "description_" + index;
        if (description.length() > 1000) {
            description = description.substring(0, 1000);
        }
        obj.setDescription(description);
    }

	public void setDisplayName(SecurityGroup obj, int index) {
        String displayName = "displayName_" + index;
        if (displayName.length() > 64) {
            displayName = displayName.substring(0, 64);
        }
        obj.setDisplayName(displayName);
    }

	public SecurityGroup getSpecificSecurityGroup(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        SecurityGroup obj = data.get(index);
        Long id = obj.getId();
        return securityGroupService.findSecurityGroup(id);
    }

	public SecurityGroup getRandomSecurityGroup() {
        init();
        SecurityGroup obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return securityGroupService.findSecurityGroup(id);
    }

	public boolean modifySecurityGroup(SecurityGroup obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = securityGroupService.findSecurityGroupEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'SecurityGroup' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<SecurityGroup>();
        for (int i = 0; i < 10; i++) {
            SecurityGroup obj = getNewTransientSecurityGroup(i);
            try {
                securityGroupService.saveSecurityGroup(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            securityGroupDao.flush();
            data.add(obj);
        }
    }
}
